package com.ai.service.cms.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.ai.common.exception.AlikAssert;
import com.ai.common.utils.CommonUtil;
import com.ai.manager.edu.cms.user.OperatorRoleManager;
import com.ai.model.edu.cms.backoperator.OperatorRole;
import com.ai.model.edu.cms.backoperator.dto.OperatorRoleDTO;
import com.ai.model.edu.cms.backoperator.vo.OperatorRoleVO;
import com.ai.model.edu.cms.backoperator.vo.RoleVO;
import com.ai.service.cms.user.OperatorRoleService;
import com.ai.service.cms.user.RoleService;

/**
 * Class Name : UserRoleServiceImpl<br>
 * 
 * Description : 用户角色service 实现类<br>
 * 
 * @author yangwb
 * @version $Revision$
 * @see
 *
 */
@Service("operatorRoleService")
public class OperatorRoleServiceImpl implements OperatorRoleService {

    @Resource
    private OperatorRoleManager operatorRoleManager;
    @Resource
    private RoleService roleService;

    /**
     * Description ： 根据dto查询用户角色关系<br>
     * 
     * yangwb
     * 
     * @param userRoleDTO
     * @return
     * @since
     * 
     */
    public List<OperatorRoleVO> findUserRoleVOsByDTO(OperatorRoleDTO userRoleDTO) {
        List<OperatorRoleVO> userRoleVOs = operatorRoleManager.findUserRoleVOsByDTO(userRoleDTO);
        // 如果用户角色不为空，封装角色信息
        if (CollectionUtils.isNotEmpty(userRoleVOs)) {
            List<Long> roleIds = CommonUtil.getValueList(userRoleVOs, "roleId");
            List<RoleVO> roleVOs = roleService.findRoleVOsByIds(roleIds);
            Map<Long, RoleVO> roleMap = CommonUtil.listforMap(roleVOs, "id", null);
            for (OperatorRoleVO operatorRoleVO : userRoleVOs) {
                if (null != roleMap && null != roleMap.get(operatorRoleVO.getRoleId())) {
                    operatorRoleVO.setRoleVO(roleMap.get(operatorRoleVO.getRoleId()));
                }
            }
        }
        return userRoleVOs;
    }

    /**
     * Description ： 用户赋角色
     * 
     * nyj
     * 
     * @param userRoles
     * @since
     *
     */
    public void addRole2User(OperatorRoleDTO operatorRoleDTO) {
        // 参数校验
        AlikAssert.isNotNull(operatorRoleDTO, "参数不能为空");
        AlikAssert.isNotNull(operatorRoleDTO.getRoleId(), "角色ID不能为空");
        AlikAssert.isNotNull(operatorRoleDTO.getCreateBy(), "操作人不能为空");
        // 根据角色ID查找角色用户信息
        Long roleId = operatorRoleDTO.getRoleId();
        OperatorRoleDTO userRoleDTO = new OperatorRoleDTO();
        userRoleDTO.setRoleId(roleId);
        List<OperatorRoleVO> operatorRoleVOs = operatorRoleManager.findUserRoleVOsByDTO(userRoleDTO);
        // 已存在的用户ID集合
        List<Long> existOperatorIds = CommonUtil.getValueList(operatorRoleVOs, "backOperatorId");
        // 需要增加角色的用户ID集合
        List<Long> toAddOperatorIds = operatorRoleDTO.getBackOperatorIds();
        // 找出需要删除的数据
        List<Long> operatorIds = new ArrayList<Long>();
        if (!CollectionUtils.isEmpty(operatorRoleVOs)) {
            for (OperatorRoleVO operatorRoleVO : operatorRoleVOs) {
                if (CollectionUtils.isEmpty(toAddOperatorIds) || !toAddOperatorIds.contains(operatorRoleVO.getBackOperatorId())) {
                    operatorIds.add(operatorRoleVO.getBackOperatorId());
                }
            }
        }

        // 找出需要新增的数据
        List<OperatorRole> operatorRoles = new ArrayList<OperatorRole>();
        if (!CollectionUtils.isEmpty(toAddOperatorIds)) {
            for (Long toAddOperatorId : toAddOperatorIds) {
                if (CollectionUtils.isEmpty(existOperatorIds) || !existOperatorIds.contains(toAddOperatorId)) {
                    OperatorRole operatorRole = new OperatorRole();
                    operatorRole.setCreateBy(operatorRoleDTO.getCreateBy());
                    operatorRole.setRoleId(roleId);
                    operatorRole.setBackOperatorId(toAddOperatorId);
                    operatorRoles.add(operatorRole);
                }
            }
        }
        // 删除
        if (!CollectionUtils.isEmpty(operatorIds)) {
            operatorRoleManager.deleteOperatorRoleByRoleIdAndOperatorId(roleId, operatorIds);
        }
        // 新增
        if (!CollectionUtils.isEmpty(operatorRoles)) {
            operatorRoleManager.addUserRoleBatch(operatorRoles);
        }
    }

    /**
     * 
     * Method Name : addUserRoleBatch<br>
     * 
     * Description : 批量新增
     * 
     * @param userRoles
     * @since
     *
     */
    public void addUserRoleBatch(List<OperatorRole> userRoles) {
        operatorRoleManager.addUserRoleBatch(userRoles);
    }

    /**
     * Description ： 根据backendOperatorId删除用户角色关系<br>
     * 
     * yangwb
     * 
     * @param backendOperatorId
     * @return
     * @since
     *
     */
    public int deleteUserRoleByBackOperatorId(Long backOperatorId) {
        return operatorRoleManager.deleteUserRoleByBackOperatorId(backOperatorId);
    }

    /**
     * Description ：新增角色角色用户关系
     * 
     * yangwb
     * 
     * @param OperatorRole operatorRole
     * @return
     * @since
     *
     */
    public void insert(OperatorRole operatorRole) {
        operatorRoleManager.insert(operatorRole);
    }

    /**
     * Description ：根据用户id和角色ids 查询该用户角色关系 <br>
     * 
     * yangwb
     * 
     * @param operatorRoleDTO
     * @return map : key:roleId;value:OperatorRoleVO
     * @since
     * 
     */
    public Map<Long, OperatorRoleVO> findOperatorRoleMap(OperatorRoleDTO operatorRoleDTO) {
        Map<Long, OperatorRoleVO> map = new HashMap<Long, OperatorRoleVO>();
        List<OperatorRoleVO> operatorRoleVOs = operatorRoleManager.findUserRoleVOsByDTO(operatorRoleDTO);
        if (!CollectionUtils.isEmpty(operatorRoleVOs)) {
            for (OperatorRoleVO operatorRoleVO : operatorRoleVOs) {
                if (map.get(operatorRoleVO.getRoleId()) == null) {
                    map.put(operatorRoleVO.getRoleId(), operatorRoleVO);
                }
            }
        }
        return map;
    }

    /**
     * Description ：根据ID删除
     * 
     * yangwb
     * 
     * @param Long id
     * @return
     * @since
     *
     */
    public void deleteById(Long id) {
        operatorRoleManager.deleteById(id);
    }

    /**
     * Description ：根据用户id 和角色ids 删除关系<br>
     * 
     * yangwb
     * 
     * @param backOperatorId
     * @param roleIds
     * @return
     * @since
     * 
     */
    public int removeOperatorRoleByRoleIdsAndOperatorId(Long backOperatorId, List<Long> roleIds) {
        return operatorRoleManager.removeOperatorRoleByRoleIdsAndOperatorId(backOperatorId, roleIds);
    }
}
